package com.guangzhi.weijianzhi.mainhome;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.adapter.MainBannerPagerAdapter;
import com.guangzhi.weijianzhi.fragment.BaseFragment;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.utils.Misc;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MainIndexPager extends BaseFragment implements OnPageChangeListener {
	private ArrayList<String> imaUrl = new ArrayList<String>();

		View mView;
		private ViewPager mViewPager;
		private int current =0;
	//	private TextView number;
		int time = 3000;

		private Handler mHandler;

		private RadioGroup radiog;
		
		@Override
		public void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			
			mHandler = new Handler(){
				
				@Override
				public void handleMessage(Message msg) {
					// TODO Auto-generated method stub
					super.handleMessage(msg);
					int curr = mViewPager.getCurrentItem();
					mViewPager.setCurrentItem(curr+1,true);
					Message msg2 = new Message();
					msg2.what = 0;
					mHandler.sendMessageDelayed(msg2, time);
				}
			};
		}
		
		
		@Override
		public View onCreateView(LayoutInflater inflater,
				ViewGroup container,Bundle savedInstanceState) {
			mView =inflater.inflate(R.layout.main_top_view_layout, null);
			initView(mView);
			return mView;
		}

		@Override
		public void onResume() {
			super.onResume();
		}
		
		@Override
		public void onStop() {
			super.onStop();
			
		}
		
		private void initView(View view) {
			mViewPager = (ViewPager)view.findViewById(R.id.pager);
		radiog = (RadioGroup) view.findViewById(R.id.bg_page_number);
			mViewPager.setOnPageChangeListener(this);
		}
		
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			if(imaUrl.size()==0){
				initData();
			}
			
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int i) {//0
			int location =i % imaUrl.size();
		//	number.setText(++location+"/"+imaUrl.size());
			radiog.check(location);
		}
			
		@SuppressWarnings("static-access")
		private void initData() {
			HttpRequestUtils.doHttpBanner(new AsyncHttpResponseHandler() {
				
				@Override
				public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
					// TODO Auto-generated method stub

	    			try {
						JSONObject jsonO = new JSONObject(new String(arg2));
						if(jsonO.optBoolean("status")){
							imaUrl.clear();
							JSONArray jsonA = jsonO.optJSONArray("data");
							for(int j=0;j<jsonA.length();j++){
								
								JSONObject jsonObj = jsonA.optJSONObject(j);
								
								imaUrl.add(jsonObj.optString("image_url"));
								imaUrl.add(jsonObj.optString("image_url"));
								imaUrl.add(jsonObj.optString("image_url"));
								imaUrl.add(jsonObj.optString("image_url"));
								
							}
							if(imaUrl.size()>0){
								radiog.removeAllViews();
								for(int j=0;j<imaUrl.size();j++){
									RadioButton rb = (RadioButton) View.inflate(getActivity(), R.layout.tz_main_radiobu_layout, null);
									rb.setId(j);
									radiog.addView(rb);
								}
								radiog.check(0);
								
							//	number.setText("1"+"/"+imaUrl.size());
								mViewPager.setAdapter(new MainBannerPagerAdapter(imaUrl,getActivity()));
								Message msg = new Message();
								msg.what = 0;
								mHandler.sendMessageDelayed(msg, time);
								
							}
						}
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				@Override
				public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {}
			});
			
			
			
		}
	}
